package com.syezon.wifikey.api;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author YL
 */
public class HttpClient {

    private static String TAG = HttpClient.class.getSimpleName();

    /**
     * 发送HTTP POST请求，获取服务器返回的接口数据（带参数）
     *
     * @param url
     * @param param
     * @return
     */
    public static String postResult(String url, String param) throws IOException {
        // 创建HTTP连接
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setDoInput(true); // 允许输入流，即允许下载，默认为true
        conn.setDoOutput(true); // Post请求需要允许输出流，即允许上传，默认为false
        conn.setUseCaches(false); //  Post请求不能使用缓存
        conn.setRequestMethod("POST"); // 使用POST请求，默认GET
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(5000);
        // 设置请求参数
        byte[] data = param.getBytes("UTF-8");
        OutputStream os = conn.getOutputStream();
        os.write(data);
        os.flush();
        os.close();
        // 进行连接，提交参数，并获取返回的数据
        String result = "";
        int responseCode = conn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            InputStream is = conn.getInputStream(); // 获取输入流，此时才真正建立链接
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader bufferReader = new BufferedReader(isr);
            String line = "";
            while ((line = bufferReader.readLine()) != null) {
                result += line + "\n";
            }
            if (bufferReader != null) {
                bufferReader.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (is != null) {
                is.close();
            }
            return result;
        } else {
        }
        return "";
    }

    /**
     * 发送HTTP get请求，获取服务器返回的接口数据（带参数）
     *
     * @param url
     * @param param
     * @return
     */

    public static String getResult(String url, String param) throws IOException {
        // 创建HTTP连接
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setConnectTimeout(5000);
        conn.setReadTimeout(5000);
        String result = "";
        int responseCode = conn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            InputStream is = conn.getInputStream(); // 获取输入流，此时才真正建立链接
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader bufferReader = new BufferedReader(isr);
            String line = "";
            while ((line = bufferReader.readLine()) != null) {
                result += line + "\n";
            }
            if (bufferReader != null) {
                bufferReader.close();
            }
            if (isr != null) {
                isr.close();
            }
            if (is != null) {
                is.close();
            }
            return result;
        } else {
        }
        return "";
    }

    /**
     * 检查获取的数据是否正确
     *
     * @param result
     * @return
     */
    public static boolean checkResult(String result) {
        if (result == null || result.equals("")) {
            return false;
        }
        try {
            JSONObject json = new JSONObject(result);
            if (json.optInt("RC") == 1 || json.optInt("rc") == 1) {
                return true;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    //以下为新后台接口


    public static void sendDebugInfo(String info) {
        try {
            JSONObject json = new JSONObject();
            json.put("debug", "####" + info);
            postResult("http://lab.qclx.com/doc/wifiwxpay/payid.htm", json.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
